package com.honeybee.cloud.framework.gateway.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.web.reactive.result.view.ViewResolver;

import java.util.Collections;
import java.util.List;

/**
 * @Description 自定义异常处理[@@]注册Bean时依赖的Bean，会从容器中直接获取，所以直接注入即可
 * @Author DELL
 * @Create 2019-02-20
 * @Since 1.0.0
 */
@Slf4j
public class CustomerGateWayConfig {
    @Primary
    @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE)
    public ErrorWebExceptionHandler errorWebExceptionHandler(ObjectProvider<List<ViewResolver>> viewResolversProvider,
                                                             ServerCodecConfigurer serverCodecConfigurer) {

        GateWayJsonExceptionHandler gateWayJsonExceptionHandler = new GateWayJsonExceptionHandler();
        gateWayJsonExceptionHandler.setViewResolvers(viewResolversProvider.getIfAvailable(Collections::emptyList));
        gateWayJsonExceptionHandler.setMessageWriters(serverCodecConfigurer.getWriters());
        gateWayJsonExceptionHandler.setMessageReaders(serverCodecConfigurer.getReaders());
        log.info("Init Json Exception Handler Instead Default ErrorWebExceptionHandler Success");
        return gateWayJsonExceptionHandler;
    }

}

